<?php 
	require_once 'libs/required.php';
	require_once 'DbTables/user.class.php';
	
	$resizeWidth 	    = 200; //resize width
	$resizeHeight 		= 200; //Thumbnail Height
	$result ='';
	if($_POST)
	{
		$email    = $_POST['email'];
		$username = $_POST['username'];
		$password = sha1($_POST['password']);
		$first_name = $_POST['first_name'];
		$last_name = $_POST['last_name'];
		$address = $_POST['address'];
		$imageNewName ='';
		//Upload avatar 
		if($_FILES['avatar']['tmp_name'] !=''){
			$ImageName 		= strtolower($_FILES['avatar']['name']);
			$ImageSize 		= $_FILES['avatar']['size']; 
			$TempSrc	 	= $_FILES['avatar']['tmp_name'];
			$ImageType	 	= $_FILES['avatar']['type'];
			//Validate file + create image from uploaded file.
			switch(strtolower($ImageType))
			{
				case 'image/png':
					$createdImage = imagecreatefrompng($_FILES['avatar']['tmp_name']);
					break;		
				case 'image/gif':
					$createdImage = imagecreatefromgif($_FILES['avatar']['tmp_name']);
					break;
				case 'image/jpeg':
					$createdImage = imagecreatefromjpeg($_FILES['avatar']['tmp_name']);
					break;
				default:
					$result ='Unsupported File!'; //output error
					break;
			}
			
			if($result =='')
			{
				list($curWidth,$curHeight)=getimagesize($TempSrc);
		
				//get file extension, this will be added after random name
				$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
			  	$ImageExt = str_replace('.','',$ImageExt);
				
				$imageNewName = $username.'.'.$ImageExt;
				resizeImage($curWidth,$curHeight,$resizeWidth,$resizeHeight,'avatars/'.$imageNewName,$createdImage);
			}
		}
		
		if($result == '')
		{
			$a_User = array($username,$password,$first_name,$last_name,$address,$email,$imageNewName);
			$result = $o_UserModel->b_fInsert($a_User);
			if($result)
			{
				$result='Sign up success';
			}	
		}
	}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Create new Account</title>
    <script src="scripts/jquery-1.9.1.min.js"></script>
    <script src="scripts/bootstrap.min.js"></script>
    <script src="scripts/jquery.validate.min.js"></script>
    <link href="styles/bootstrap.css" rel="stylesheet" />
    <link href="styles/site.css" rel="stylesheet" />
    <link href="styles/img/favicon.ico" rel="shortcut icon" />
</head>
<body>
   <div class="container">
      <form class="form-signin" action="register.php" method="POST" id="input_form" enctype="multipart/form-data">
      		<?php if($result)
				{
					echo '<h3>'.$result.'</h3>';		
				}else 
				{
					echo '<h2 class="form-signin-heading">Please sign up</h2>';
				}
			?>
        	<input type="text" placeholder="Username" class="input-block-level" name="username" id="username"  class="required"/>
        	<input type="password" placeholder="Password" class="input-block-level" name="password" id="password"  class="required"/>
        	<input type="password" placeholder="Confirm password" class="input-block-level" name="repassword" id="repassword"  class="required"  equalTo="#password"/>
 			<input type="text" placeholder="Email address" class="input-block-level" name="email" id="email"  class="required email"/>
        	<input type="text" placeholder="First Name" class="input-block-level" name="first_name" id="first_name" class="required"/>
        	<input type="text" placeholder="Last Name" class="input-block-level" name="last_name" id="last_name" class="required"/>
        	<input type="text" placeholder="Address" class="input-block-level" name="address" id="address" class="required"/>
        	<input type="file" placeholder="Avatar" class="input-block-level" name="avatar" id="avatar" class="required"/>
       		<br/>
       		<button type="submit" name="submit" class="btn btn-large btn-primary btn-success">Create Account</button>
       		<div><a href="login.php">Login</a></div>
      </form>

    </div>
</body>
</html>
<script>
	$(document).ready(function(){
	    var validator = $("#input_form").validate({ 
	        rules: {  
	    		username: "required",
	            password: { 
	                required: true, 
	            }, 
	            repassword: { 
	                equalTo: "#password"
	            }, 
	            email: { 
	                required: true, 
	                email: true
	            },
	            first_name: "required",
	            last_name: "required",
	            address: "required",
	        }, 
	        messages: { 
	        	username: "Username is  required.",
		        email: { 
	                required: "Email is required", 
	                email:"Invalid email address"
	            }, 
	            password: { 
	                required: "Password is required."
	            },
	            repassword: { 
	                equalTo: "Password confirm is not matching." 
	            }, 
	            first_name: "First name is  required.",
	            last_name: "Last name is  required.",
	            address: "Address is  required."
	        }
	    }); 
	});
</script>